package com.mashibing.dp.strategy;

import java.util.Arrays;

/**
 * writing tests first!
 * extreme programming
 * 策略模式：能让你定义一系列算法，并将每种算法分别放入独立的类中，以使算法的对象能够相互替换
 */
public class Main {
    public static void main(String[] args) {
        //int[] a = {9, 2, 3, 5, 7, 1, 4};
        //
        Cat[] a = {new Cat(3, 3), new Cat(5, 5), new Cat(1, 1)};
        Sorter<Cat> sorter = new Sorter<>();
        sorter.sort(a,new CatHeightComparator());
        System.out.println(Arrays.toString(a));

        Dog[] b = {new Dog(3), new Dog(5), new Dog(1)};
        Sorter<Dog> sorter2 = new Sorter<>();
        sorter2.sort(b,new DogComparator());
        System.out.println(Arrays.toString(b));

        // Lambda表达式:当一个接口中要实现的方法只有一个时可以用
        // 功能等同 CatWeightComparator
        Sorter<Cat> sorter3 = new Sorter<>();
        sorter3.sort(a, (o1, o2)->{
            if(o1.weight < o2.weight) return -1;
            else if (o1.weight>o2.weight) return 1;
            else return 0;
        });
        System.out.println(Arrays.toString(a));
    }
}
